/*
 * Copyright (C) 2011, EADS France
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.gmi.view.diagram;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 *
 * @author Laurent WOUTERS
 */
public class Circuit extends Shape {
    private List<Couple> vertices;
    
    public Circuit(int[] x, int[] y, java.awt.Color backColor, java.awt.Color borderColor, int borderThickness, boolean isFill) {
        super(getMax(x), getMax(y), backColor, borderColor, borderThickness, isFill);
        vertices = new ArrayList<>(x.length);
        for (int i=0; i!=x.length; i++)
            vertices.add(new Couple(x[i], y[i]));
    }
    
    private static int getMax(int[] values) {
        int max = 0;
        for (int i=0; i!=values.length; i++) {
            if (values[i] > max)
                max = values[i];
        }
        return max;
    }
    
    public List<Couple> getVertices() {
        if (!isVisible()) return emptyVertices;
        List<Couple> result = new ArrayList<>(vertices);
        Collections.sort(result, new Comparator<Couple>(){
            @Override public int compare(Couple p1, Couple p2) {
                int c = Double.compare(p1.x, p2.x);
                if (c != 0) return c;
                return Double.compare(p1.y, p2.y);
            }
        });
        return result;
    }
    
    @Override public void paint(java.awt.Graphics2D g2d, float alpha, DiagramStyle style, Couple offset) {
        int[] dx = new int[vertices.size()];
        int[] dy = new int[vertices.size()];
        Couple temp = offset.plus(position);
        for (int i=0; i!=vertices.size(); i++) {
            Couple v = vertices.get(i).plus(temp);
            dx[i] = (int)v.x;
            dy[i] = (int)v.y;
        }
        g2d.setColor(getRealColor((backColor!=null)? backColor: style.shapeBackgroundColor, alpha));
        g2d.fillPolygon(dx, dy, vertices.size());
        g2d.setColor(getRealColor((borderColor!=null)? borderColor: style.shapeBorderColor, alpha));
        g2d.setStroke(new java.awt.BasicStroke((borderThickness!=0)? borderThickness: style.shapeBorderThickness));
        g2d.drawPolygon(dx, dy, vertices.size());
    }
    @Override protected void exportSVG(org.w3c.dom.Document doc, org.w3c.dom.Element parent, float alpha, DiagramStyle style, Couple offset) {
        if (!isVisible()) return;
        java.lang.StringBuilder builder = new java.lang.StringBuilder();
        for (int i=0; i!=vertices.size(); i++) {
            if (i!=0) builder.append(" ");
            Couple v = position.plus(offset).plus(vertices.get(i));
            builder.append(v.x);
            builder.append(",");
            builder.append(v.y);
        }
        org.w3c.dom.Element elem = doc.createElement("polygon");
        elem.setAttribute("points", builder.toString());
        java.awt.Color fill = getRealColor((backColor!=null)? backColor: style.shapeBackgroundColor, alpha);
        java.awt.Color border = getRealColor((borderColor!=null)? borderColor: style.shapeBorderColor, alpha);
        int stroke = (borderThickness!=0)? borderThickness: style.shapeBorderThickness;
        if (fill != null && fill.getAlpha() != 0) {
            elem.setAttribute("fill", "#" + getColorString(fill));
            if (fill.getAlpha() != 255) {
                float value = ((float)fill.getAlpha()) / 255;
                elem.setAttribute("fill-opacity", Float.toString(value));
            }
        }
        if (stroke > 0 && border != null && border.getAlpha() != 0) {
            elem.setAttribute("stroke", "#" + getColorString(border));
            if (border.getAlpha() != 255) {
                float value = ((float)border.getAlpha()) / 255;
                elem.setAttribute("stroke-opacity", Float.toString(value));
            }
            elem.setAttribute("stroke-width", Integer.toString(stroke));
        }
        parent.appendChild(elem);
    }
}
